import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'From',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyStatelessWidget(),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {

  GlobalKey _formKey = new GlobalKey<FormState>();

  TextEditingController _usernameController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    //定义文本样式

    return Scaffold(
      appBar: AppBar(
        title: Text("From"),
      ),
      body: Column(
        children: <Widget>[
          Form(
            key: _formKey,
            autovalidate: true,
            child: Column(
              children: <Widget>[
                TextFormField(
                  autofocus: true,
                  decoration: InputDecoration(
                    labelText:"用户名",
                    hintText: "请输入用户名",
                    icon: Icon(Icons.person)
                  ),
                  validator: (val){
                    if(val.trim().length < 6 ){
                      return "您输入的数据不能小于6位";
                    }
                    return val.trim().length > 0 ? null:'请输入用户名';
                  },
                  controller: _usernameController,
                ),
                RaisedButton(
                  child: Text("提交"),
                  onPressed: (){
                    if((_formKey.currentState as FormState).validate()){
                      print("提交数据给后台：" + _usernameController.text);
                    }
                  },
                )
              ],
            ),
          )
        ],
      ),
    );
  }

}
